# Sources are located relative to Makefile
MAKEFILE_DIR := $(shell readlink -f $(dir $(word $(words $(MAKEFILE_LIST)), $(MAKEFILE_LIST))))

# The path of the invoking module
vpath %.c $(MAKEFILE_DIR)
vpath %.cpp $(MAKEFILE_DIR)

# default target
default: all

include $(MAKEFILE_DIR)/../make.inc

# Only support MINGW64_NT x86_64
ifeq "$(OSARCHIVE)" "MINGW64_NT"
ifeq "$(OSRELEASE)" "3.0.7"
EXPORT_DYNAMIC := --export-all-symbols
OS_INCLUDE += -I/mingw64/include -I/mingw64/include/c++/9.2.0/backward -I/mingw64/include/c++/9.2.0
OS_INCLUDE += -I/mingw64/x86_64-w64-mingw32/include -I/mingw64/lib/gcc/x86_64-w64-mingw32/9.2.0/include-fixed -I/mingw64/lib/gcc/x86_64-w64-mingw32/9.2.0/include
OS_LDFLAGS += -Wl,-rpath,/mingw64/lib/gcc/x86_64-w64-mingw32/9.2.0:/mingw64/lib/gcc:/mingw64/x86_64-w64-mingw32/lib:/mingw64/libexec:/mingw64/lib
OS_LDLIBS += -L/mingw64/lib/gcc/x86_64-w64-mingw32/9.2.0 -L/mingw64/lib/gcc -L/mingw64/x86_64-w64-mingw32/lib -L/mingw64/libexec -L/mingw64/lib
endif
endif

PROJECT_INC := $(MAKEFILE_DIR)/../inc
PROJECT_LIB := $(MAKEFILE_DIR)/../lib

# CXX Compiler Flags
CFLAGS += -pthread
CXXFLAGS += -pthread
INCLUDE += -iquote$(MAKEFILE_DIR) -I. -I$(PROJECT_INC)
INCLUDE += $(OS_INCLUDE)
DEFINES += -DUNICODE -D_UNICODE

# CXX Linker Flags
LDFLAGS += -pthread
LDFLAGS += -Wl,$(EXPORT_DYNAMIC) -Wl,-rpath,.:$(MAKEFILE_DIR):$(PROJECT_LIB)
LDFLAGS += $(OS_LDFLAGS)
LDLIBS += -L$(MAKEFILE_DIR) -L. -L$(PROJECT_LIB)
LDLIBS += $(OS_LDLIBS)

# Object Sources
SRCDIRS = .
CSRC += $(wildcard $(SRCDIRS:%=$(MAKEFILE_DIR)/%/*.c))
CSRC := $(strip $(subst $(SRCDIRS:%=$(MAKEFILE_DIR)/%/test_main.c),,$(CSRC)))
CXXSRC += $(wildcard $(SRCDIRS:%=$(MAKEFILE_DIR)/%/*.cpp))
CXXSRC := $(strip $(subst $(SRCDIRS:%=$(MAKEFILE_DIR)/%/test_main.cpp),,$(CXXSRC)))
COBJ = $(CSRC:$(MAKEFILE_DIR)/%.c=%.o)
CXXOBJ = $(CXXSRC:$(MAKEFILE_DIR)/%.cpp=%.o)
OBJS = $(COBJ) $(CXXOBJ)

.PHONY: default all clean

all: hello.exe

hello.exe: $(OBJS)
	$(LD) -static $(LDFLAGS) $^ $(LDLIBS) -o $@

clean: 
	$(RM) hello.exe
